<?
	$page["title"]="Actions";
	$page["file"]="actions.html";

	include "include/config.inc";
	show_header($page["title"],0);
?>

<?
	if(isset($register))
	{
		if($register=="add")
		{
			add_action( $triggerid, $userid, $good, $delay, $subject, $message );
		}
		if($register=="update")
		{
			update_action( $actionid, $userid, $good, $delay, $subject, $message );
			unset($actionid);
		}
		if($register=="delete")
		{
			delete_action($actionid);
			unset($actionid);
		}
	}
?>

<?
	$sql="select description, expression from triggers where triggerid=$triggerid";
	$result=DBselect($sql);
	$description=DBget_field($result,0,0);
	$expression=DBget_field($result,0,1);
?>
 
<?
	$expression=explode_exp($expression,1);
	show_table_header("$description<BR>$expression");
?>

<hr>

<?
	$sql="select a.actionid,a.triggerid,u.alias,a.good,a.delay,a.subject,a.message from actions a,users u where a.userid=u.userid and a.triggerid=$triggerid order by u.alias, a.good desc";
	$result=DBselect($sql);

	echo "<CENTER>";
	echo "<TABLE BORDER=0 WIDTH=100% BGCOLOR=\"#CCCCCC\" cellspacing=1 cellpadding=3>";
	echo "<TR>";
	echo "<TD><b>Send message to</b></TD>";
	echo "<TD><b>When trigger</b></TD>";
	echo "<TD><b>Delay</b></TD>";                            
	echo "<TD><b>Subject</b></TD>";
	echo "<TD><b>Message</b></TD>";
	echo "<TD><b>Actions</b></TD>";                               
	echo "</TR>";
	$col=0;
	for($i=0;$i<DBnum_rows($result);$i++)
	{
		$actid=DBget_field($result,$i,0);
		$good="OFF";
		if(DBget_field($result,$i,3))
		{
			$good="ON";
		}
		$to=DBget_field($result,$i,2);
		$delay=DBget_field($result,$i,4);
		$subject=DBget_field($result,$i,5);
		$msg=DBget_field($result,$i,6);

		if(isset($actionid) && ($actionid==$actid))
		{
			echo "<TR BGCOLOR=#FFDDDD>";
			$col=$col+1;
			$sactid=$actid;
			$sgood=$good;
			$sto=$to;
			$sdelay=$delay;
			$ssubject=$subject;
			$smsg=$msg;
		} 
		else
		{
			if($col==1) 
			{
				echo "<TR BGCOLOR=#EEEEEE>";
				$col=0;
			} else
			{
				echo "<TR BGCOLOR=#DDDDDD>";
				$col=1;
			}
		}
  
		echo "<TD>$to</TD>";
		echo "<TD>$good</TD>";
		echo "<TD>$delay</TD>";
		echo "<TD>$subject</TD>";
		echo "<TD>$msg</TD>";
		echo "<TD>";
		echo " <A HREF=\"actions.html?register=edit&actionid=$actid&triggerid=$triggerid\">Edit</A>";
		echo ", <A HREF=\"actions.html?register=delete&actionid=$actid&triggerid=$triggerid\">Delete</A>";
		echo "</TD></TR>";
	}
	echo "</TABLE>";
?>
</font>
</tr>
</table></center>

<?

	if(isset($actionid))
	{
		$sql="select a.actionid,a.triggerid,a.good,a.delay,a.subject,a.message from actions a where a.actionid=$actionid";
		$result=DBselect($sql);

		$actionid=DBget_field($result,0,0);
		$triggerid=DBget_field($result,0,1);
		$good=DBget_field($result,0,2);
		$delay=DBget_field($result,0,3);
		$subject=DBget_field($result,0,4);
		$message=DBget_field($result,0,5);
	}
	else
	{
		$good=1;
		$delay=30;
		$subject="";
		$message="";
	}
	echo "<br>";
	show_table2_header_begin();
	echo "New action";

	show_table2_v_delimiter();
	echo "<form method=\"post\" action=\"actions.html\">";
	echo "<input name=\"triggerid\" type=\"hidden\" value=$triggerid>";
	if(isset($actionid))
	{
		echo "<input name=\"actionid\" type=\"hidden\" value=$actionid>";
	}
	echo "Send message to";
	show_table2_h_delimiter();
	echo "<SELECT NAME=\"userid\" SIZE=\"1\">";

	$sql="select userid,alias from users order by alias";
	$result=DBselect($sql);
	for($i=0;$i<DBnum_rows($result);$i++)
	{
		$id=DBget_field($result,$i,0);
		$name=DBget_field($result,$i,1);
		if(isset($sto) && ($name==$sto))
		{
			echo "<option value=\"$id\" SELECTED>$name";
		}
		else
		{
			echo "<option value=\"$id\">$name";
		}
	}
	echo "</select>";

	show_table2_v_delimiter();
	echo "When trigger becomes";
	show_table2_h_delimiter();
	echo "<select name=\"good\" size=1>";
	echo "<OPTION VALUE=\"1\""; if($good==1) echo "SELECTED"; echo ">ON";
	echo "<OPTION VALUE=\"0\""; if($good==0) echo "SELECTED"; echo ">OFF";
	echo "</SELECT>";

	show_table2_v_delimiter();
	echo "Delay";
	show_table2_h_delimiter();
	echo "<input name=\"delay\" value=\"$delay\" size=5>";

	show_table2_v_delimiter();
	echo "Subject";
	show_table2_h_delimiter();
	echo "<input name=\"subject\" value=\"$subject\" size=50>";

	show_table2_v_delimiter();
	echo "Message";
	show_table2_h_delimiter();
	echo "<input name=\"message\" value=\"$message\" size=50>";

	show_table2_v_delimiter2();
	echo "<input type=\"submit\" name=\"register\" value=\"add\">";
	if(isset($actionid))
	{
		echo "<input type=\"submit\" name=\"register\" value=\"update\">";
	}

	show_table2_header_end();

	show_footer();
?>
